package com.jiangyg.mall.admin.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jiangyg.mall.admin.constant.ColumnConstant;
import com.jiangyg.mall.admin.dao.PermissionDao;
import com.jiangyg.mall.admin.dao.RoleDao;
import com.jiangyg.mall.admin.dto.PermissionDTO;
import com.jiangyg.mall.admin.entity.Permission;
import com.jiangyg.mall.admin.entity.Role;
import com.jiangyg.mall.admin.enums.PermissionType;
import com.jiangyg.mall.authz.UserInfoHolder;
import com.jiangyg.mall.core.enums.Enable;
import com.jiangyg.mall.core.exception.BusinessException;
import com.jiangyg.mall.core.utils.I18n;
import com.jiangyg.mall.db.mybatis.BaseServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PermissionServiceImpl extends BaseServiceImpl<PermissionDao, Permission> implements PermissionService {

    private final RoleDao roleDao;

    @Autowired
    public PermissionServiceImpl(PermissionDao permissionDao, RoleDao roleDao) {
        super(permissionDao);
        this.roleDao = roleDao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveRolePermission(PermissionDTO dto, Long loginUserId) {
        final Long roleId = dto.getRoleId();
        // 1. 验证是否有修改权限
        {
            // 角色信息
            final Role role = this.roleDao.selectById(roleId);
            if (role == null) {
                throw new BusinessException(I18n.getString("mall.admin.role.not.exist"));
            }
            // 验证是否有修改权限：1、超级管理员    2、角色创建者
            if (!UserInfoHolder.isAdmin(loginUserId) && !loginUserId.equals(role.getCreateUserId())) {
                throw new BusinessException(I18n.getString("mall.admin.role.update.havePermission"));
            }
        }
        // 2. 删除已有权限
        {
            final List<Long> deleteResourceIds = dto.getDeleteResourceIds();
            if (CollectionUtils.isNotEmpty(deleteResourceIds)) {
                final UpdateWrapper<Permission> wrapper = new UpdateWrapper<>();
                wrapper.eq(ColumnConstant.REFERENCE_ID, roleId);
                wrapper.in(ColumnConstant.RESOURCE_ID, deleteResourceIds);
                wrapper.eq(ColumnConstant.TYPE, PermissionType.R.code);
                wrapper.eq(ColumnConstant.ENABLE, Enable.Y.scode);
                this.baseMapper.delete(wrapper);
            }
        }
        // 3. 新增权限信息
        {
            final List<Long> addResourceIds = dto.getAddResourceIds();
            if (CollectionUtils.isNotEmpty(addResourceIds)) {
                final List<Permission> permissions = addResourceIds.stream()
                        .map(id -> new Permission(roleId, id, PermissionType.R.code)).collect(Collectors.toList());
                this.saveOrUpdateBatch(permissions, 20);
            }
        }
        return true;
    }

}
